/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PrivilegesPanel.java
 *
 * Created on 2011-04-05, 11:40:00
 */
package bsk_dac;

import bsk_dac.accesscontrol.TableAccess;
import bsk_dac.accesscontrol.AccessType;
import bsk_dac.users.User;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JCheckBox;

/**
 * Jest to panel reprezentjący grupę praw dostępu "Access".
 */
public class PrivilegesPanel extends javax.swing.JPanel {

    private TableAccess access = new TableAccess();
    private User user;
    private String tableName;

    /** Creates new form PrivilegesPanel */
    public PrivilegesPanel(User user, String tableName) {
        initComponents();
        this.user = user;
        this.tableName = tableName;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        selectCB = new javax.swing.JCheckBox();
        updateCB = new javax.swing.JCheckBox();
        deleteCB = new javax.swing.JCheckBox();
        insertCB = new javax.swing.JCheckBox();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        selectCB.setText("Select");
        selectCB.setEnabled(false);
        add(selectCB);

        updateCB.setText("Update");
        updateCB.setEnabled(false);
        add(updateCB);

        deleteCB.setText("Delete");
        deleteCB.setEnabled(false);
        add(deleteCB);

        insertCB.setText("Insert");
        insertCB.setEnabled(false);
        add(insertCB);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox deleteCB;
    private javax.swing.JCheckBox insertCB;
    private javax.swing.JCheckBox selectCB;
    private javax.swing.JCheckBox updateCB;
    // End of variables declaration//GEN-END:variables

    public User getUser() {
        return user;
    }

    public String getTableName() {
        return tableName;
    }

    public void setAccess(TableAccess access) {
        this.access = access;
    }

    public TableAccess getAccess() {
        return this.access;
    }

    public void changeSelectAccess(boolean acc) {
        this.access.setPrivileges(AccessType.SELECT, acc);
    }

    public void changeUpdateAccess(boolean acc) {
        this.access.setPrivileges(AccessType.UPDATE, acc);
    }

    public void changeDeleteAccess(boolean acc) {
        this.access.setPrivileges(AccessType.DELETE, acc);
    }

    public void changeInsertAccess(boolean acc) {
        this.access.setPrivileges(AccessType.INSERT, acc);
    }

    public void refreshAccessView() {
        if (access == null) {
            access = new TableAccess();
        }
        setUnselectedAllCheckboxes();
    }

    public void setCheckboxState() {
        setCheckboxState(access, true, false, true);
    }

    public void setEnabledAllCheckboxes() {
        selectCB.setEnabled(true);
        updateCB.setEnabled(true);
        deleteCB.setEnabled(true);
        insertCB.setEnabled(true);

        selectCB.setVisible(true);
        updateCB.setVisible(true);
        deleteCB.setVisible(true);
        insertCB.setVisible(true);
    }



    public void setCheckboxState(TableAccess access, boolean setEnabled, boolean setVisible, boolean setSelected) {

        if (setEnabled == true) {
            if (access.getPrivilegesStatus(AccessType.SELECT)) {

                selectCB.setEnabled(true);
            } else {
                selectCB.setEnabled(false);
            }
            if (access.getPrivilegesStatus(AccessType.UPDATE)) {

                updateCB.setEnabled(true);
            } else {
                updateCB.setEnabled(false);
            }
            if (access.getPrivilegesStatus(AccessType.DELETE)) {
                deleteCB.setEnabled(true);
            } else {
                deleteCB.setEnabled(false);
            }
            if (access.getPrivilegesStatus(AccessType.INSERT)) {

                insertCB.setEnabled(true);
            } else {
                insertCB.setEnabled(false);
            }
        }
        if (setSelected == true) {
            if (access.getPrivilegesStatus(AccessType.SELECT)) {
                if (access.getUserGivePermission(AccessType.SELECT) == false) {
                    selectCB.setBackground(Color.red);
                    selectCB.setForeground(Color.red);
                }
                selectCB.setSelected(true);
            } else {
                selectCB.setSelected(false);
            }
            if (access.getPrivilegesStatus(AccessType.UPDATE)) {
                if (access.getUserGivePermission(AccessType.UPDATE) == false) {
                    updateCB.setBackground(Color.red);
                    updateCB.setForeground(Color.red);
                }
                updateCB.setSelected(true);
            } else {
                updateCB.setSelected(false);
            }
            if (access.getPrivilegesStatus(AccessType.DELETE)) {
                if (access.getUserGivePermission(AccessType.DELETE) == false) {
                    deleteCB.setBackground(Color.red);
                    deleteCB.setForeground(Color.red);
                }
                deleteCB.setSelected(true);
            } else {
                deleteCB.setSelected(false);
            }
            if (access.getPrivilegesStatus(AccessType.INSERT)) {
                if (access.getUserGivePermission(AccessType.INSERT) == false) {
                    insertCB.setBackground(Color.red);
                    insertCB.setForeground(Color.red);
                }
                insertCB.setSelected(true);
            } else {
                insertCB.setSelected(false);
            }
        }

        if (setVisible == true) {
            if (access.getPrivilegesStatus(AccessType.SELECT)) {
                selectCB.setVisible(true);
            } else {
                selectCB.setVisible(false);
            }
            if (access.getPrivilegesStatus(AccessType.UPDATE)) {
                updateCB.setVisible(true);
            } else {
                updateCB.setVisible(false);
            }
            if (access.getPrivilegesStatus(AccessType.DELETE)) {
                deleteCB.setVisible(true);
            } else {
                deleteCB.setVisible(false);
            }
            if (access.getPrivilegesStatus(AccessType.INSERT)) {
                insertCB.setVisible(true);
            } else {
                insertCB.setVisible(false);
            }
        }
    }

    public void hideSelected(){
        if (access.getPrivilegesStatus(AccessType.SELECT)) {
                selectCB.setEnabled(false);
                selectCB.setSelected(true);
            }
            if (access.getPrivilegesStatus(AccessType.UPDATE)) {
               updateCB.setEnabled(false);
                updateCB.setSelected(true);
            }
            if (access.getPrivilegesStatus(AccessType.DELETE)) {
               deleteCB.setEnabled(false);
                deleteCB.setSelected(true);
            }
            if (access.getPrivilegesStatus(AccessType.INSERT)) {
               insertCB.setEnabled(false);
                insertCB.setSelected(true);
            }
    }

    public void hideRed(TableAccess access) {
        selectCB.setBackground(Color.GRAY);
        updateCB.setBackground(Color.gray);
        deleteCB.setBackground(Color.gray);
        insertCB.setBackground(Color.gray);

        selectCB.setForeground(Color.BLACK);
        updateCB.setForeground(Color.BLACK);
        deleteCB.setForeground(Color.BLACK);
        insertCB.setForeground(Color.black);

        if (access.getPrivilegesStatus(AccessType.SELECT)) {
            if (access.getUserGivePermission(AccessType.SELECT) == false) {
                selectCB.setVisible(false);
            }
            selectCB.setEnabled(true);
        } else {
            selectCB.setEnabled(false);
        }

        if (access.getPrivilegesStatus(AccessType.UPDATE)) {
            if (access.getUserGivePermission(AccessType.UPDATE) == false) {
                updateCB.setVisible(false);
            }
            updateCB.setEnabled(true);
        } else {
            updateCB.setEnabled(false);
        }

        if (access.getPrivilegesStatus(AccessType.DELETE)) {
            if (access.getUserGivePermission(AccessType.DELETE) == false) {
                deleteCB.setVisible(false);
            }
            deleteCB.setEnabled(true);
        } else {
            deleteCB.setEnabled(false);
        }

        if (access.getPrivilegesStatus(AccessType.INSERT)) {
            if (access.getUserGivePermission(AccessType.INSERT) == false) {
                insertCB.setVisible(false);
            }
            insertCB.setEnabled(true);
        } else {
            insertCB.setEnabled(false);
        }
    }

    public void setCheckboxStateForGetPrivileges(TableAccess access) {
        if (access.getPrivilegesStatus(AccessType.SELECT) && !selectCB.isSelected()) {
            selectCB.setEnabled(true);
        } else {
            selectCB.setEnabled(false);
        }
        if (access.getPrivilegesStatus(AccessType.UPDATE) && !updateCB.isSelected()) {
            updateCB.setEnabled(true);
        } else {
            updateCB.setEnabled(false);
        }
        if (access.getPrivilegesStatus(AccessType.DELETE) && !deleteCB.isSelected()) {
            deleteCB.setEnabled(true);
        } else {
            deleteCB.setEnabled(false);
        }
        if (access.getPrivilegesStatus(AccessType.INSERT) && !insertCB.isSelected()) {
            insertCB.setEnabled(true);
        } else {
            insertCB.setEnabled(false);
        }
    }

    public void setVisibleStatusAsAccess(TableAccess access) {
        if (access.getPrivilegesStatus(AccessType.SELECT)) {
            selectCB.setVisible(true);
        } else {
            selectCB.setVisible(false);
        }
        if (access.getPrivilegesStatus(AccessType.UPDATE)) {
            updateCB.setVisible(true);
        } else {
            updateCB.setVisible(false);
        }
        if (access.getPrivilegesStatus(AccessType.DELETE)) {
            deleteCB.setVisible(true);
        } else {
            deleteCB.setVisible(false);
        }
        if (access.getPrivilegesStatus(AccessType.INSERT)) {
            insertCB.setVisible(true);
        } else {
            insertCB.setVisible(false);
        }
    }

    public JCheckBox getSelectCB() {
        return selectCB;
    }

    public JCheckBox getUpdateCB() {
        return updateCB;
    }

    public JCheckBox getDeleteCB() {
        return deleteCB;
    }

    public JCheckBox getInsertCB() {
        return insertCB;
    }

    public void showAccessInCheckBox() {
        selectCB.setSelected(access.getPrivilegesStatus(AccessType.SELECT));
        updateCB.setSelected(access.getPrivilegesStatus(AccessType.UPDATE));
        deleteCB.setSelected(access.getPrivilegesStatus(AccessType.DELETE));
        insertCB.setSelected(access.getPrivilegesStatus(AccessType.INSERT));

        selectCB.setVisible(access.getPrivilegesStatus(AccessType.SELECT));
        updateCB.setVisible(access.getPrivilegesStatus(AccessType.UPDATE));
        deleteCB.setVisible(access.getPrivilegesStatus(AccessType.DELETE));
        insertCB.setVisible(access.getPrivilegesStatus(AccessType.INSERT));
    }

    private void setUnselectedAllCheckboxes() {
        selectCB.setSelected(false);
        updateCB.setSelected(false);
        deleteCB.setSelected(false);
        insertCB.setSelected(false);
    }

    public void setGreenPermissions(ArrayList<String> permissionNames){
        for(String permissionName : permissionNames){
            if(permissionName.equals("select"))
            {
                selectCB.setBackground(Color.GREEN);
                selectCB.setEnabled(true);
            }
            if(permissionName.equals("insert"))
            {
                insertCB.setBackground(Color.GREEN);
                insertCB.setEnabled(true);
            }
            if(permissionName.equals("delete"))
            {
                deleteCB.setBackground(Color.GREEN);
                deleteCB.setEnabled(true);
            }
            if(permissionName.equals("update"))
            {
                updateCB.setBackground(Color.GREEN);
                updateCB.setEnabled(true);
            }
        }
    }

    public void setEnabledAllCheckboxes(boolean enabled) {
        selectCB.setEnabled(enabled);
        updateCB.setEnabled(enabled);
        deleteCB.setEnabled(enabled);
        insertCB.setEnabled(enabled);
    }

    public void setSelectedAllCheckboxes(boolean enabled) {
        selectCB.setSelected(enabled);
        updateCB.setSelected(enabled);
        deleteCB.setSelected(enabled);
        insertCB.setSelected(enabled);
    }

    public void setBlackAllCheckboxes(){
        selectCB.setBackground(Color.BLACK);
        insertCB.setBackground(Color.BLACK);
        deleteCB.setBackground(Color.BLACK);
        updateCB.setBackground(Color.BLACK);
    }

}
